pipeline {
    // 所有机器
    agent any
    // 工具 需要现在全局工具里面配置
    tools {
        maven 'M3'
    }

    environment {
        PATH = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/bin/date"
    }

    parameters {
        choice(
                choices: 'test',
                description: '请选择你要部署的环境',
                name: 'deploy_env'
        )
        string(name: 'branch', defaultValue: 'test', description: '需要部署的分支名称')
        string(name: 'email_user', defaultValue: 'xyqierkang@163.com', description: '邮件通知用户')
        string(name: 'image_prefix', defaultValue: 'registry-vpc.cn-shanghai.aliyuncs.com/qekmidd/', description: '镜像前缀')
        string(name: 'git_url', defaultValue: 'git@gitee.com:moneypro/lancaster-parent.git', description: '仓库地址')
    }


    stages {
        stage('pull deploy code') {
            steps {
                echo '==================1.开发环境准备================================'
                echo '1.1请确保安装并java8, git, maven并设置好环境变量'
                echo '1.2请确保安装并docker,docker-compose并设置好环境变量'
                echo '==================1.Pull deploy code=================='
                sh 'git config --global http.sslVerify false'
                dir("${env.WORKSPACE}") {
                    git branch: '$branch', credentialsId: '1a52cef8-da44-4df8-8294-b25e974288cb', url: '$git_url'
                }
            }
        }

        stage('build deploy code') {
            steps {
                echo '==================2.build deploy code=================='
                sh 'mvn --version'
                sh 'cd lancaster-utils && mvn clean install -Dmaven.test.skip=true -Ptest'
                script {
                    sh 'cd ${modular_name} && mvn clean install -Dmaven.test.skip=true -Ptest'
                }
            }
        }
        stage('push deploy code') {
            steps {
                echo '==================3.push deploy code=================='
                script {
                    env.gitCommit = sh(script: 'git rev-parse --short HEAD ${GIT_COMMIT}', returnStdout: true).trim()
                    echo "Hello Image ：${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP} --build-arg GIT_COMMIT=${gitCommit}"
                    sh 'cd ${modular_name} && docker build -t ${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP} .'
                    sh 'docker push ${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP}'
                }

            }
        }
        stage('update run service') {
            steps {
                script {
                    echo '==================4.update run service=================='
                    echo '======================执行拉取新镜像========================'
                    sh 'docker pull ${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP}'
                    echo '======================执行拉取server01镜像完成======================='
                    sh 'docker service update --image ${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP} ${modular_name}-test'
                    sh 'docker service ps ${modular_name}-test'
                    echo '======================执行镜像服务server01更新完成===================='
                    echo '======================执行删除旧server01镜像操作======================'
                    sh 'docker ps -aq --filter name=${modular_name}-test --filter status=exited'

                }
            }
        }

        stage("set currentBuild") {
            steps {
                script {
                    currentBuild.description = "${image_prefix}${modular_name}-test:${BUILD_TIMESTAMP}"
                }
            }
        }

    }

    post {
        always {
            //always部分 pipeline运行结果为任何状态都运行
            emailext body:
                    '''<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                        <table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                            <tr>
                                <td>
                                    <b><font color="#0B610B">Build Summary</font></b>
                                    <hr size="2" width="100%" align="center" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <ul>
                                        <li>Project Name: ${PROJECT_NAME}</li>
                                        <li>Build Number: # ${BUILD_NUMBER}</li>
                                        <li>Build Cause: ${CAUSE}</li>
                                        <li>Build Status: ${BUILD_STATUS}</li>
                                        <li>Build Log: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                                        <li>Build Url: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                                        <li>Project Url: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                                        <li>Docker Image: ${image_prefix}:${BUILD_TIMESTAMP}</a></li>
                                        <li>Git Commit: ${CHANGES_SINCE_LAST_SUCCESS , showPaths = true}</a></li>
                                    </ul>
                                </td>
                            </tr>
                        </table>
                   </body>''',
                    subject: '[Jenkins Build Notification] ${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!'
        }
        success {
            //当此Pipeline成功时打印消息
            echo '==========================SUCCESS=========================='
            echo 'Author: ErKang Qi'
            echo 'Blog: https://www.qekang.com'
        }
        failure {
            //当此Pipeline失败时打印消息
            echo 'failure'
        }
        unstable {
            //当此Pipeline 为不稳定时打印消息
            echo 'unstable'
        }
        aborted {
            //当此Pipeline 终止时打印消息
            echo 'aborted'
        }
        changed {
            //当pipeline的状态与上一次build状态不同时打印消息
            echo 'changed'
        }
    }

}